package com.pacifictextiles.qc.inspection.server.controller.servlets;


import java.util.List;
import com.pacifictextiles.qc.inspection.server.model.vo.Batch;
import com.pacifictextiles.qc.inspection.server.model.vo.Defect;
import com.pacifictextiles.qc.inspection.server.model.vo.Roll;
import com.pacifictextiles.qc.inspection.server.model.vo.Waste;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONPackager {
	
	public JSONObject packageBatch(Batch batch) {
		
		// 创建JSONObject对象
		JSONObject batchJSONObject = new JSONObject();
		
		// 写入数据到JSONObject
		batchJSONObject.put("fabricType", batch.getFabricType());
		batchJSONObject.put("dispoNo", batch.getDispoNo());
		batchJSONObject.put("batchNo", batch.getBatchNo());
		batchJSONObject.put("repairNo", batch.getRepairNo());
		batchJSONObject.put("colorCode", batch.getColorCode());
		batchJSONObject.put("customerCode", batch.getCustomerCode());
		batchJSONObject.put("customerPO", batch.getCustomerPO());
		batchJSONObject.put("dispoLineNo", batch.getDispoLineNo());
		batchJSONObject.put("fabricCode", batch.getFabricCode());
		batchJSONObject.put("fullCode", batch.getFullCode());
		batchJSONObject.put("qualityTable", batch.getQualityTable());
		batchJSONObject.put("pieceSample", batch.getPieceSample());
		batchJSONObject.put("ppSample", batch.getPpSample());
		batchJSONObject.put("shipSample", batch.getShipSample());
		batchJSONObject.put("shrinkageSample", batch.getShrinkageSample());
		batchJSONObject.put("fabricCorrectionFactor", batch.getFabricCorrectionFactor());
		batchJSONObject.put("lotNo", batch.getLotNo());
		
		// 调用packageRollList方法获取rollList的对应JSONArray
		batchJSONObject.put("rollList", packageRollList(batch.getRollList()));
		
		return batchJSONObject;
	}
	
	public JSONArray packageRollList(List<Roll> rollList) {
		
		JSONArray rollJSONArray = new JSONArray();
		
		for (Roll roll:rollList) {
			// 一个roll的对应JSONObject
			JSONObject obj = new JSONObject();
			
			// 赋值
			obj.put("rollNo", roll.getRollNo());
			obj.put("cpi", roll.getCpi());
			obj.put("expRollNo", roll.getExpRollNo());
			obj.put("dateAndTime", roll.getDateAndTime());
			obj.put("defectCount", roll.getDefectCount());
			obj.put("expRollNo", roll.getExpRollNo());
			obj.put("fabricWeight", roll.getFabricWeight());
			obj.put("fullCode", roll.getFullCode());
			obj.put("grossLength", roll.getGrossLength());
			obj.put("netLength", roll.getNetLength());
			obj.put("operatorId", roll.getOperatorId());
			obj.put("qualityLevel", roll.getQualityLevel());
			obj.put("space", roll.getSpace());
			obj.put("stationId", roll.getStationId());
			obj.put("totalPoints", roll.getTotalpoints());
			obj.put("tubeWeight", roll.getTubeWeight());
			obj.put("width", roll.getWidth());
			obj.put("wasteLength", roll.getWasteLength());
			obj.put("wpi", roll.getWpi());
			obj.put("inspectStatus", roll.isInspectStatus());
			obj.put("weightingStatus", roll.isWeightingStatus());
			
			// 调用packageDefectList和packageWasteList获得相应JSONArray
			obj.put("defectList", packageDefectList(roll.getDefectList()));
			obj.put("wasteList", packageWasteList(roll.getWasteList()));
			
			rollJSONArray.add(obj);
		}		
		
		return rollJSONArray;
	}
	
	public JSONArray packageDefectList(List<Defect> defectList) {
		
		JSONArray defectJSONArray = new JSONArray();
		
		for (Defect defect:defectList) {
			JSONObject obj = new JSONObject();
			
			obj.put("rollNo", defect.getRollNo());
			obj.put("defectCode", defect.getDefectCode());
			obj.put("point", defect.getPoint());
			obj.put("position", defect.getPosition());
			obj.put("rating", defect.getRating());
			obj.put("dateAndTime", defect.getDateAndTime());
			
			defectJSONArray.add(obj);
		}		
		return defectJSONArray;
	}
	
	public JSONArray packageWasteList(List<Waste> wasteList) {
		
		JSONArray wasteJSONArray = new JSONArray();
		
		for (Waste waste:wasteList) {
			JSONObject obj = new JSONObject();
			
			obj.put("rollNo", waste.getRollNo());
			obj.put("dateAndTime", waste.getDateAndTime());
			obj.put("wasteLength", waste.getWasteLength());
			obj.put("endPosition", waste.getEndPosition());
			obj.put("beginPosition", waste.getBeginPosition());
			obj.put("defectCode", waste.getDefectCode());
			
			wasteJSONArray.add(obj);
		}		
		return wasteJSONArray;
	}

	public JSONObject packageRollNoAndIndex(String[] rollNoAndIndex) {
		
		// 创建JSONObject对象
		JSONObject rollNoAndIndexJSONObject = new JSONObject();
	
		// 写入数据到JSONObject
		rollNoAndIndexJSONObject.put("rollNo", rollNoAndIndex[0]);
		rollNoAndIndexJSONObject.put("indexInBatch", rollNoAndIndex[1]);
		
		return rollNoAndIndexJSONObject;
	}
}

